<template>
  <div class="goodsdetail">

    <div class="banner">
      <van-swipe class="my-swipe"
                 :autoplay="3000"
                 indicator-color="white">
        <van-swipe-item v-for="(item,index) in gallery"
                        :key="index">
          <img :src="item.img_url"
               alt="">
        </van-swipe-item>

      </van-swipe>
    </div>
    <nav-item></nav-item>
    <div class="name">

      <div class="title">
        {{info.name}}
      </div>
      <div class="desc">
        {{info.goods_brief}}
      </div>
      <div class="price">
        ￥ {{info.retail_price}}
      </div>
    </div>
    <div class="sheet"
         @click="openSheet">
      <div>请选择规格数量</div>
      <div> > </div>
    </div>
    <!-- 弹出层内容 -->
    <van-action-sheet v-model="showSheet"
                      :round=false
                      @close="close">
      <div class="content">
        <div class="top">
          <img :src="info.primary_pic_url"
               alt="">
          <div>
            价格:{{info.retail_price}}
          </div>
        </div>
        <div class="number">
          <span>购买数量:</span>
          <van-stepper v-model="number"
                       step="1" />
        </div>
      </div>
    </van-action-sheet>
    <div class="pro-detail">
      <div class="pro-c">
        <div class="title">
          商品参数
        </div>
        <div class="list">
          <div class="item"
               v-for="(item,index) in attribute"
               :key="index">

            <span class="name">{{item.name}}</span>
            <span>{{item.value}}</span>
          </div>
        </div>
      </div>

    </div>
    <div class="detail-desc"
         v-html="info.goods_desc">

    </div>
    <div class="use-answer">
      <div class="user-c">
        <div class="header">
          <div class="line"></div>
          <div class="name">常见问题</div>
          <div class="line"></div>
        </div>
        <div class="list">
          <div class="item"
               v-for="(item, index) in issue"
               :key="index">
            <div class="quesion">
              <div class="dot"></div>
              <div> {{item.question}}</div>

            </div>
            <div class="anser">
              {{item.answer}}
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="pro-like">
      <div class="like-c">
        <div class="header">
          <div class="line"></div>
          <div class="name">大家都在看</div>
          <div class="line"></div>

        </div>
        <div class="list">
          <div class="item"
               v-for="(item,index) in productList"
               :key="index">
            <img :src="item.list_pic_url"
                 alt="">
            <div class="title">{{item.name}}</div>
            <div class="price"> ￥ {{item.retail_price}}</div>
          </div>
        </div>
      </div>
    </div>
    <van-goods-action>
   
      <van-goods-action-icon icon="star"
                             text="收藏"
                             @click="showcol"
                            v-if="showcollet"
                             color="#ff5000" />
                                <van-goods-action-icon icon="star-o"
                             text="收藏"
                             @click="showcol"
                             v-else
                             color="black" />
      <van-goods-action-icon icon="cart-o"
                             :badge="carlength"
                             @click="goShopcar"
                             text="购物车" />

      <van-goods-action-button type="danger"
                               text="立即购买" />
      <van-goods-action-button type="warning"
                               text="加入购物车"
                               @click="openSheet" />
    </van-goods-action>
  </div>
</template>

<script>
import NavItem from "./Nav"
import homeApi from "@/netapi/home/home.js"

import shopcar from "@/netapi/shopcar/shopcar.js"

import { mapState, mapActions } from "vuex"
import store from '@/store'
export default {
  name: "GoodsDetail",
  components: {
    NavItem
  },
  data () {
    return {
      id: "",
      attribute: [],
      comment: {},
      gallery: [],
      info: {},
      issue: [],
      productList: [],
      showSheet: false,
      number: 1,
      count: 0,
      showcollet: true,
      badge: "",
      shop: []

    };
  },
  computed: {
    carlength () {
      return this.shop.reduce((pr, nx) => {
        return pr + nx.number
      }, 0)
    }
  },
  created () {
    this.id = this.$route.params.id;
    this.getGoodDetail()
    this.$store.dispatch("getColllist", this.id)
    this.$store.dispatch("getShopcar")
    this.getLength()
  },
  mounted () {

  },
  methods: {
    getGoodDetail () {
      homeApi.goodsDetail({
        id: this.id,
        openId: JSON.parse(window.localStorage.getItem("userInfo")).openId
      }).then(res => {
        console.log(res);
        this.attribute = res.attribute;
        this.comment = res.comment;
        this.gallery = res.gallery;
        this.info = res.info;
        this.issue = res.issue;
        this.productList = res.productList;
        this.showcollet = this.$store.state.showcoll;
        this.badge = this.$store.state.carLength

      })

    },

    openSheet () {
      this.showSheet = true;
      this.count++
      console.log(this.count);
      if (this.count > 1) {
        shopcar.addCar({
          goodsId: this.id,
          number: this.number,
          openId: JSON.parse(window.localStorage.getItem("userInfo")).openId
        }).then(res => {
          console.log(res);
          this.$store.dispatch("getShopcar")
          this.getLength()

        })

      }
    },
    // 关闭清零
    close () {
      this.count = 0
    },
    showcol () {
      this.showcollet = !this.showcollet
      console.log(111);
      shopcar.addcollet({
        goodsId: this.id,
        openId: JSON.parse(window.localStorage.getItem("userInfo")).openId
      }).then(res => { console.log(res); })
    },
    // 获取购物车长度
    getLength () {
      shopcar.Carlist({
        openId: JSON.parse(localStorage.getItem("userInfo")).openId
      }).then(res => {
        this.badge = res.data.length
        this.shop = res.data
      })
    },
    //跳转购物车
    goShopcar(){
      this.$router.push({name:"Shopcar"})
    }
  },
};
</script>

<style scoped lang="scss">
.goodsdetail {
  width: 100%;
  .banner {
    width: 100%;
    height: 377px;
    .my-swipe .van-swipe-item {
      img {
        width: 100%;
        height: 377px;
      }
    }
  }
  .name {
    width: 100%;
    text-align: center;
    border-bottom: 2px solid #f4f4f4;
    .title {
      margin-top: 15px;
      margin-bottom: 37px;
      font-size: 20px;
      font-weight: bold;
    }
    .desc {
      font-size: 12px;
      color: #ceccc6;
    }
    .price {
      margin-top: 20px;
      font-size: 25px;
      color: #c03f43;
    }
  }
  .sheet {
    width: 95%;
    margin: 0 auto;

    height: 54px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
  }
  .content {
    width: 96%;
    height: 230px;
    margin: 0 auto;
    margin-top: 15px;
    .top {
      display: flex;
      align-items: center;

      img {
        width: 100px;
        height: 100px;
        margin-right: 30px;
        margin-left: 20px;
      }
    }
    .number {
      margin-top: 30px;
    }
  }

  .pro-detail {
    width: 100%;
    border-top: 10px solid #f4f4f4;
    .pro-c {
      width: 93%;
      margin: 0 auto;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      word-break: break-all;
      .title {
        text-align: left;
        font-size: 19px;
        margin-top: 20px;
        width: 100%;
      }
      .list {
        width: 100%;
        height: 300px;
        .item {
          width: 100%;
          height: 50px;
          line-height: 50px;
          font-size: 14px;
          background-color: #f7f7f7;
          margin-bottom: 8px;
          .name {
            color: #bcbcbc;
            margin-right: 5px;
          }
        }
      }
    }
  }
  .detail-desc {
    width: 100%;
    text-align: center;

    /deep/ p {
      margin: 0;
      padding: 0;
    }
    /deep/ img {
      display: block;
      width: 100%;
    }
  }
  .use-answer {
    width: 100%;
    .user-c {
      width: 95%;
      margin: 0 auto;
      .header {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 30px;
        .line {
          width: 60px;
          height: 1px;
          background-color: black;
        }
        .name {
          width: 70px;
        }
      }
      .list {
        width: 100%;
        margin-top: 20px;
        .item {
          margin-top: 20px;
          .quesion {
            height: 20px;
            font-size: 14px;
            line-height: 20px;
            display: flex;
            align-items: center;
            margin-bottom: 10px;
            .dot {
              width: 5px;
              height: 5px;

              background-color: red;
              border-radius: 100%;
              margin-right: 5px;
            }
          }
          .anser {
            line-height: 2;
          }
        }
      }
    }
  }
  .pro-like {
    width: 100%;
    border-top: 56px solid #f4f4f4;
    .like-c {
      width: calc(100vw - 4px);
      margin: 0 auto;
      .header {
        height: 50px;
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;

        .name {
          font-size: 14px;
          width: 80px;
          margin: 0 12px;
        }
        .line {
          width: 50px;
          height: 2px;
          background-color: rgb(240, 204, 204);
        }
      }
      .list {
        display: flex;
        justify-content: space-around;
        background-color: #f4f4f4;
        flex-wrap: wrap;
        .item {
          height: 213px;
          width: 180px;
          background-color: #fff;
          margin-bottom: 3px;
          img {
            width: 180px;
            height: 152px;
            margin-bottom: 10px;
          }
          .price,
          .title {
            margin-left: 8px;
          }
          .price {
            color: brown;
            margin-top: 15px;
          }
        }
      }
      .list::after {
        content: "";
        flex: auto; /* 或者flex: 1 */
      }
    }
  }
  /deep/.van-button::before {
    border-radius: 0;
  }
  /deep/.van-goods-action {
    z-index: 9999;
  }
}
</style>
